#include "tcpClient.h"

void main() 
{

    // Initialize Winsock (Windows Sockets).
	WSAData wsaData; 
	if (NO_ERROR != WSAStartup(MAKEWORD(2,2), &wsaData))
	{
		cout << "Web Client: Error at WSAStartup()\n";
		return;
	}

	// Create a socket and connect to an internet address.
    SOCKET connSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == connSocket)
	{
        cout << "TimWebe Client: Error at socket(): " << WSAGetLastError() << endl;
        WSACleanup();
        return;
	}

	sockaddr_in server;
    server.sin_family = AF_INET; 
	server.sin_addr.s_addr = inet_addr("192.168.1.105");
	server.sin_port = htons(HTTP_PORT);

	// Connect to server.
	if (SOCKET_ERROR == connect(connSocket, (SOCKADDR *) &server, sizeof(server)))
	{
		cout << "Web Client: Error at connect(): " << WSAGetLastError() << endl;
        closesocket(connSocket);
		WSACleanup();
        return;
    }

    // Send and receive data.
    int bytesSent = 0;
    int bytesRecv = 0;
    char sendBuff[MAX_SIZE];
    char recvBuff[MAX_SIZE];
	int option = 0;

	while(option != 6)
	{
		cout<<"\nPlease choose an option:\n";
		cout<<"1 - Test Get.\n";
		cout<<"2 - Test Delete.\n";
		cout<<"3 - Test Head.\n";
		cout<<"4 - Test Trace.\n";
		cout<<"5 - Test Put.\n";
		cout<<"6 - Exit.\n";
		cout<<">> ";

		cin>>option;

		string testStr;

		if(option == 1)
			testStr = BuildGetStrTest();
		else if(option == 2)
			testStr = BuildDeleteStrTest();
		else if(option == 3)
			testStr = BuildHeadStrTest();
		else if(option == 4)
			testStr = BuildTraceStrTest();
		else if(option == 5)
			testStr = BuildPutStrTest();
		else if(option == 6)
			testStr= "Exit";

		strcpy(sendBuff, testStr.c_str());

		bytesSent = send(connSocket, sendBuff, (int)strlen(sendBuff), 0);
		if (SOCKET_ERROR == bytesSent)
		{
			cout << "Web Client: Error at send(): " << WSAGetLastError() << endl;
			closesocket(connSocket);
			WSACleanup();
			return;
		}	
		cout<<"Web Client: Sent: "<<bytesSent<<"/"<<strlen(sendBuff)<<" bytes of \""<<endl<<sendBuff<<"\""<<endl;

		// Gets the server's answer
		if(option != 6)
		{
			bytesRecv = recv(connSocket, recvBuff, MAX_SIZE, 0);
			if (SOCKET_ERROR == bytesRecv)
			{
				cout << "Web Client: Error at recv(): " << WSAGetLastError() << endl;
				closesocket(connSocket);
				WSACleanup();
				return;
			}
			if (bytesRecv == 0)
			{
				cout<<"Server closed the connection\n";
				return;
			}
			
			recvBuff[bytesRecv]='\0'; //add the null-terminating to make it a string
			cout<<"Web Client: Recieved: "<<bytesRecv<<" bytes of \"" <<endl<<recvBuff<<"\""<<endl;
		}
		else //Option 6
		{
			// Closing connections and Winsock.
			cout << "Web Client: Closing Connection.\n";
			closesocket(connSocket);
			WSACleanup();
		}
	}
}

string BuildGetStrTest()
{
	string TestStr;
	TestStr.append("GET /test.html HTTP/1.1");
	TestStr.append(CR_LF);
	TestStr.append("HOST: TEST");
	TestStr.append(CR_LF);
	TestStr.append("Content-Type: 5");
	TestStr.append(CR_LF);
	TestStr.append("Connection: keep-alive");
	TestStr.append(CR_LF);
	TestStr.append(CR_LF);
	return TestStr;
}

string BuildDeleteStrTest()
{
	string TestStr;
	TestStr.append("DELETE /fileToDelete.txt HTTP/1.1");
	TestStr.append(CR_LF);
	TestStr.append("HOST: TEST");
	TestStr.append(CR_LF);
	TestStr.append("Content-Type: 5");
	TestStr.append(CR_LF);
	TestStr.append("Connection: keep-alive");
	TestStr.append(CR_LF);
	TestStr.append(CR_LF);
	return TestStr;
}

string BuildHeadStrTest()
{
	string TestStr;
	TestStr.append("HEAD /test.html HTTP/1.1");
	TestStr.append(CR_LF);
	TestStr.append("HOST: TEST");
	TestStr.append(CR_LF);
	TestStr.append("Content-Type: 5");
	TestStr.append(CR_LF);
	TestStr.append("Connection: keep-alive");
	TestStr.append(CR_LF);
	TestStr.append(CR_LF);
	return TestStr;
}

string BuildTraceStrTest()
{
	string TestStr;
	TestStr.append("TRACE http://www.test.com HTTP/1.1");
	TestStr.append(CR_LF);
	TestStr.append("HOST: TEST");
	TestStr.append(CR_LF);
	TestStr.append("Content-Type: 5");
	TestStr.append(CR_LF);
	TestStr.append("Connection: keep-alive");
	TestStr.append(CR_LF);
	TestStr.append(CR_LF);
	TestStr.append("....Echo Test...");
	TestStr.append(CR_LF);
	return TestStr;
}

string BuildPutStrTest()
{
	string TestStr;
	TestStr.append("PUT /fileToWrite.txt HTTP/1.1");
	TestStr.append(CR_LF);
	TestStr.append("HOST: TEST");
	TestStr.append(CR_LF);
	TestStr.append("Content-Type: 5");
	TestStr.append(CR_LF);
	TestStr.append("Connection: keep-alive");
	TestStr.append(CR_LF);
	TestStr.append(CR_LF);
	TestStr.append("->Some data to write:");
	TestStr.append(CR_LF);
	TestStr.append("...File Data info...");
	TestStr.append(CR_LF);
	TestStr.append("-End of data- ");
	TestStr.append(CR_LF);
	return TestStr;
}